import {defineStore} from 'pinia'
import {message} from 'ant-design-vue';
import zjRequest from "@/services";
import {IActions, IState, URL_TYPE} from "@/store/departModule/types";
import {IResponseData} from "@/services/types";

const useDepartStore = defineStore<any, IState, any, IActions>('departModule', {
  state: () => {
    return {
      departList: [],
    }
  },
  actions: {
    // 获取部门列表
    async requestDepartListAction() {
      const res: IResponseData = await zjRequest.get({
        url: URL_TYPE.DEPART_LIST,
        method: "POST"
      })
      if (res.code === 0) {
        this.departList = res.data
      }
    },
    // 添加部门
    async addDepartAction(params) {
      const res: IResponseData = await zjRequest.post({
        url: URL_TYPE.DEPART_ADD,
        data: params
      })
      if (res.code === 0) { // 添加成功
        message.success("添加成功")
        this.requestDepartListAction()
        return true
      } else {
        return false
      }
    },
    // 删除部门
    async deleteDepartAction(params) {
      const res = await zjRequest.post({
        url: URL_TYPE.DEPART_DELETE,
        data: params
      })
      if (res.code === 0) {// 删除成功
        message.success("删除成功")
        this.requestDepartListAction()
      }
    },
    // 修改部门
    async updateDepartAction(params) {
      const res = await zjRequest.post({
        url: URL_TYPE.DEPART_UPDATE,
        data: params
      })
      if (res.code === 0) {
        message.success("修改成功")
        this.requestDepartListAction()
        return true
      } else {
        return false
      }
    }
  }
})
export default useDepartStore
